package com.easthome.controller;

import com.easthome.common.RespResult;
import com.easthome.constant.Constants;
import com.easthome.entity.Goods;
import com.easthome.exception.BussinessException;
import com.easthome.pojo.vo.GoodsVO;
import com.easthome.service.GoodsService;
import com.easthome.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;

/**
 * 前端发起ajax请求，来到后端，后端处理完成后，会有数据要返回给前端
 * 有时返回的数据是个"ok"，有时可能没有数据，有时可能是list
 * 后端统一 返回一个名叫 RespResult对象
 * @author cxiaoy
 * @Date 2022/10/14 14:13
 */
@RestController
@RequestMapping("/goodsManagement")
public class GoodsManagementController {

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("/goodsList")
    public RespResult goodsList(){
        System.out.println("goodsList....");
        List<GoodsVO> list = goodsService.goodsList();
//        return RespResult.success(list);、
        return new RespResult("ok","200",list);
    }

    @RequestMapping("/goods/{id}")
    public RespResult<Goods> getGoodsById(@PathVariable Integer id){
        System.out.println("getGoodsById...."+id);
        Goods goods = goodsService.goodsById(id);
        return RespResult.success(goods);
    }

    /*
    @RequestPart：获取请求的一部分，
    上传文件时，可以使用这个注解
    uploadFile为空，没有上传这事。。直接修改
    uploadFile不为空但过大，抛异常
    uploadFile不为空也不过大，上传完，再修改。
     */
    @RequestMapping("/updateGoods")
    public RespResult updateGoods(@RequestPart Goods goods, @RequestPart(required = false) MultipartFile uploadFile) throws IOException {
        System.out.println("updateGoods...."+goods);
        System.out.println("updateGoods...."+uploadFile);
        //上传文件,先判断一下。
        if(uploadFile!=null && uploadFile.getSize()> Constants.UPLOAD_FILE_MAX_SIZE){
            throw new BussinessException("文件过大","9527");
        }
        if(uploadFile!=null){
            //上传,并返回一个新的文件名，
            String newFilename = FileUtil.upload(uploadFile);
            goods.setImgUrl(Constants.DEPLOY_FILE_PATH+ File.separator+newFilename);
        }
        //修改数据库。
        int r = goodsService.updateGoods(goods);

//        Goods goods = goodsService.goodsById(id);
//        return RespResult.success(goods);
        return RespResult.success();
    }

    @RequestMapping("/addGoods")
    public RespResult addGoods(@RequestPart Goods goods, @RequestPart(required = false) MultipartFile uploadFile) throws IOException {
        System.out.println("addGoods...."+goods);
        System.out.println("addGoods...."+uploadFile);
        //上传文件,先判断一下。
        if(uploadFile!=null && uploadFile.getSize()> Constants.UPLOAD_FILE_MAX_SIZE){
            throw new BussinessException("文件过大","9527");
        }
        if(uploadFile!=null){
            //上传,并返回一个新的文件名，
            String newFilename = FileUtil.upload(uploadFile);
            goods.setImgUrl(Constants.DEPLOY_FILE_PATH+ File.separator+newFilename);
        }
//        else{//多处理了一下，前端多传的"static/"
//            String imgUrl = goods.getImgUrl();
//            goods.setImgUrl(imgUrl.substring(imgUrl.indexOf("/")+1));
//            System.out.println("------------"+goods.getImgUrl());
//        }
        //修改数据库。
        int r = goodsService.addGoods(goods);
        return RespResult.success();
    }

    @DeleteMapping("/deleteGoods/{goodsId}")
    public RespResult deleteGoods(@PathVariable Integer goodsId){
        System.out.println("deleteGoods...."+goodsId);
        int r = goodsService.deleteGoods(goodsId);
        return RespResult.success();//delete from 表 where.....
    }

    @RequestMapping("/deleteBatch")
    public RespResult deleteBatch(@RequestBody List<Integer> ids){
        System.out.println("deleteBatch...."+ids);
        int r = goodsService.deleteBatch(ids);
        return RespResult.success();//delete from 表 where.....
    }
}
